<?
class view
{
	public $include;
	public $table;
	public $index;
	public $indexname; 
	public $title;
	public $query;
	public $properties;

   
/**
 * Creates a view from an array relates to the db
 * array looks like this 
 * properties = 
 *              [headers] 	array of header names;
 * 				[properties] array of
 * 								[style] 	name of the style
 * 								[editurl]		edit enabled or not value should be 
 * 								[eparam]	array of parameters, values from the table should be put up the same way it's in the table
 * 								[deleteurl]		delete enabled
 * 								[deleteparam]	array of parameters parametername=>rowname
 *  * 										
 */
function CreateView(){
	if($this->query){
		$res = dbQuery($this->query) or die(dbError());
		if (!$this->properties['cols'])
			$this->properties['cols'] = count($this->properties['headers']) +2;
		//create the table
		$output .= "<table width=100% class='".$this->properties['style']."'  id='".$this->properties['tid']."'>";
		if($this->title)
		$output .="<thead>";
		$output .=  "<tr bgcolor=#CCCCCC><th colspan=".$this->properties['cols']." nowrap><font size=3 face=arial>".$this->title."</font></th></tr>";
			if(is_array($this->properties['headers'])){
				$output .= "<tr bgcolor=#CCCCCC>";
				while(list($htext,$value) = each ($this->properties['headers'])){
					$output .= "<th nowrap>";
					$output .= "<b>".$htext."</b>";
					$output .= "</th>";
                                        $farr [] = $value;
				}
				$output .= "</tr>";
			}
		$output .= "</thead>";
		//adapting to format
		$color = '#FFFFFF';
		$output .="<tbody>";
                
                //fill an array with the results
                while($row = dbFetchArray($res)){
                        $results[] = $row;
                };
                
                
                //format the internal part of the table.
                if(is_array($results)){
		foreach($results as $row){
                        
			$output .= "<tr";
			if($color == '#FFFFFF')
				$color = '#FFFFcc';
			else
				$color = '#FFFFFF';
			
			
			$output .= " bgcolor='".$color."'>";
			$hcount = 0;
			//get the table going 
			while ($hcount < count($farr)){
                                list($key, $value) = each($row);
				$output .= "<td>";
				//get the count of header description
				if($farr[$hcount])
					$content = $this->ReplaceKeys($row,$farr[$hcount]);
				else
					$content = $value;
				
				
				$output .= $content;
                                
				if($hcount == $this->properties['calctotal']){
                                        $total[$hcount] += $content;
				}
                                
				$output .= "</td>";
				$hcount ++;
			}
			//edit url is specific, create html for it
				if ($this->properties['editurl']){
				//create the edit link
					if($this->properties['editparam'])
						$link = $this->CreateWebQuery($this->properties['editparam'],$this->properties['editurl']);
					
				//replace the keys with actual values
				$link = $this->ReplaceKeys($row,$link);
				//echo the edit link
					$output .= "<td><a href='".$link."'>Edit</a></td>";	
				}
					
					
				if ($this->properties['deleteurl']){
				//create the delete link
					if($this->properties['deleteparam'])
						$link = $this->CreateWebQuery($this->properties['deleteparam'],$this->properties['deleteurl']);
					
				//replace the keys with values
				$link = $this->ReplaceKeys($row,$link);
				
				//echo the delete link
				$output .= "<td><a href='".$link."'>Delete</a></td>";	
				}
			$output .= "</tr>";
		}
                }
                if($this->properties['calctotal']){
                $output .="<tr>";
                $output .="<td><b>Total</b></td>";
                //put the total column
                for ($i=1; $i<$this->properties['calctotal'];$i++)
                        $output .= "<td></td>";
                
                //put the actual number
                $output .="<td>".$total[$this->properties['calctotal']]."</td>";
                $output .="</tr>";
                }
                
		$output .= "</tbody>";
		$output .= "</table>";
	}
	//apply sortable table / jquery
	$output .="<script language='javascript'>";
	$output .="$(document).ready(function()"; 
	$output .="{"; 
        $output .="$(\"table\").tablesorter();";
	$output .="} ); ";
	$output .="</script>";
    
	//return everything
	return $output;
}

/**
	 * Creating a web query string valid from parameters (array)
	 * 
	 */
function CreateWebQuery($input,$url){
		$string = "?";
		
		//create the string
		if(is_array($input)){
			while(list($key,$value) = each($input)){
				$string .= $key."=".$value."&";		
			}
		}else{
			return false;
		}
		
		//validate the string end
		if(substr($string,-1,1) == "&")
				$string = substr($string,0,strlen($string)-1);
				
		//add the url
		$string = $url.$string;
		return $string;
	}
	
	/**
	 * Replaces keys placed between ## from the row in the db with actual values according to the query
	 *
	 * @param dbrow $row
	 * @param the string $string
	 * @return link, a string
	 */
	function ReplaceKeys($row,$string){
		reset($row);
		while(list($key,$value) = each($row)){
			$keys[] = "##".$key."##";
			$values[] = $value;	
		}
		$link = str_replace($keys,$values,$string);
	return $link;
	}
	
}

?>